package com.itheima.config;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.validation.annotation.Validated;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

//@Component
//@EnableconfigurationProperties与@Component不能同时使用
@Data
//与application.yml里的属性匹配(支持松散绑定)
//@ConfigurationProperties绑定属性支持属性名宽松绑定
//注意：宽松绑定不支持注解@Value引用单个属性的方式
//pom.xml里可以引用spring-boot-configuration-processor依赖,可以去掉红框的报错
@ConfigurationProperties(prefix = "servers")
//2.开启对当前bean的属性注入校验
@Validated
public class ServerConfig {
    private String ipAddress;
    //3.设置具体的规则
    @Max(value = 8888,message = "最大值不超过8888")
   // @Min(value = 202,message = "最小值不能低于202")
    private int port;
    private long timeout;
    @DurationUnit(ChronoUnit.HOURS)
    private Duration serverTimeOut;
    @DataSizeUnit(DataUnit.KILOBYTES)
    private DataSize dataSize;
}
